package com.itboye.pondteam.utils.languageutil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;

import com.itboye.pondteam.utils.LogUtils;

import java.util.Locale;

/**
 * https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
 */
public class LocaleManager {

    public static final String LANGUAGE_ENGLISH = "en";
    public static final String LANGUAGE_CHINESE = "zh";
    public static final String LANGUAGE_IT = "it";
    private static final String LANGUAGE_KEY = "language_key";

    public static Context setLocale(Context c) {
        return updateResources(c, getLanguage(c));
    }

    //根据手机版本号进行设置语言环境
    public static Context setNewLocale(Context c, String language) {
        persistLanguage(c, language);
        return updateResources(c, language);
    }

    //SharedPreferences获取语言环境
    public static String getLanguage(Context c) {
        String lan = getLanguageForApp(c);
        return lan;
    }

    @SuppressLint("ApplySharedPref")
    private static void persistLanguage(Context c, String language) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
        // use commit() instead of apply(), because sometimes we kill the application process immediately
        // which will prevent apply() to finish
        prefs.edit().putString(LANGUAGE_KEY, language).commit();
    }


    private static Context updateResources(Context context, String language) {
        Locale locale = null;
        if (language.contains("-"))
            locale = new Locale(language.split("-")[0], language.split("-")[1]);
        else
            locale = new Locale(language);
        Locale.setDefault(locale);
        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        //做版本兼容性判断
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            //点进去看方法详情
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }

    public static String getLanguageForApp(Context base) {
//        if (base.getPackageName().toLowerCase().contains("pondlink".toLowerCase()) || base.getPackageName().toLowerCase().contains("pondteam".toLowerCase())) {
//            return "en";
        return getLanguageForPondTeam(base);
//        }
//        //获取系统当前使用的语言
//        String lan = Locale.getDefault().getLanguage();
//        //设置成简体中文的时候，getLanguage()返回的是zh,getCountry()返回的是cn.
//        //获取区域
//        String country = Locale.getDefault().getCountry();
//        LogUtils.v("ApplicationTag", "language=" + lan + "~~~~" + country);
//        if (lan.contains("_")) {
//            return lan.replace("_", "-");
//        }
//        if (lan.startsWith("zh")) {
////            if (!country.equalsIgnoreCase("cn")) {
////                country = "cn";
////            }
//            return lan + "_" + country;
//        } else if (lan.equals("en")) {
//            return lan + "_GB";
//        } else {
//            return "en_GB";
//        }
    }

    private static String getLanguageForPondTeam(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
//        String language = prefs.getString(LANGUAGE_KEY, "");
        String language = prefs.getString(LANGUAGE_KEY, "en_GB");//打包PondTeam PondLink需要
        LogUtils.v("ApplicationTag", "language=" + language);
        if (language.isEmpty()) {
            language = Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry();
//            language = "en_GB";
        } else if (language.contains("_") || language.contains("-")) {

        } else {
            language = "en_GB";
        }
        return language;
    }

    public static Locale getLocale(Resources res) {
        Configuration config = res.getConfiguration();
        return Build.VERSION.SDK_INT >= 24 ? config.getLocales().get(0) : config.locale;
    }
}